package com.mobiletrain.org.motherkitch.sliding;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.lidroid.xutils.DbUtils;
import com.lidroid.xutils.exception.DbException;
import com.mobiletrain.org.motherkitch.MyApplication;
import com.mobiletrain.org.motherkitch.R;
import com.mobiletrain.org.motherkitch.sliding.caipu.Caipu;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Type;
import java.text.SimpleDateFormat;
import java.util.Date;

public class CreateActivity extends ActionBarActivity {

    private ImageView mIcon;
    private TextView mName;
    private TextView mShort;
    private TextView mMater;
    private TextView mStep;
    private Button mCreate;
    private Toolbar mBar;
    private File mFile;
    private EditText editText;
    private TextView title;
    private TextView cancel;
    private TextView confirm;
    private View layout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_create);
        initUI();
        initBar();

    }

    //初始化
    public void initUI() {
        mBar = (Toolbar) findViewById(R.id.create_title);
        mIcon = (ImageView) findViewById(R.id.create_icon);
        getPhoto();
        mName = (TextView) findViewById(R.id.create_name);

        addName();
        mShort = (TextView) findViewById(R.id.short_msg);
        addCookName();
        mMater = (TextView) findViewById(R.id.add_mater);
        addMaterName();
        mStep = (TextView) findViewById(R.id.add_step);
        addStepName();
        mCreate = (Button) findViewById(R.id.upload_create);
        mCreate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mName.getText() != null) {
                    String name = mName.getText().toString();
                    if (mShort.getText() != null) {
                        String msg = mShort.getText().toString();
                        if (mMater.getText() != null) {
                            String mat = mMater.getText().toString();
                            if (mStep.getText() != null) {
                                String step = mStep.getText().toString();
                                Caipu caipu = new Caipu();
                                caipu.setName(name);
                                caipu.setSmsg(msg);
                                caipu.setMaterial(mat);
                                caipu.setMake(step);
                                DbUtils dbUtils = ((MyApplication) getApplication()).getDbUtils();
                                try {
                                    dbUtils.save(caipu);
                                } catch (DbException e) {
                                    e.printStackTrace();
                                }
                                Toast.makeText(CreateActivity.this, "添加成功！", Toast.LENGTH_SHORT).show();
                            } else {
                                Toast.makeText(CreateActivity.this, "步骤不能为空！", Toast.LENGTH_SHORT).show();
                            }
                        } else {
                            Toast.makeText(CreateActivity.this, "用料不能为空！", Toast.LENGTH_SHORT).show();
                        }
                    } else {
                        Toast.makeText(CreateActivity.this, "菜谱简介不能为空！", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(CreateActivity.this, "菜谱不能为空！", Toast.LENGTH_SHORT).show();
                }
            }
        });
        mFile = new File(getExternalCacheDir(), "aaa.png");


    }

    public void initBar() {
        setSupportActionBar(mBar);
        mBar.setNavigationIcon(R.drawable.back);
        mBar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CreateActivity.this.finish();
            }
        });
    }

    public void getPhoto() {
        mIcon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(CreateActivity.this);
                builder.setItems(new String[]{"从相册选取", "拍照获取"}, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        switch (which) {
                            case 0:
//                                Intent i = new Intent(Intent.ACTION_PICK,
//                                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
//                                //调用android的图库
                                Intent i = new Intent();
                                i.addCategory(Intent.CATEGORY_OPENABLE);
                                i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mFile));
                                i.setType("image/*");
                                //根据版本号不同使用不同的Action
                                if (Build.VERSION.SDK_INT < 19) {
                                    i.setAction(Intent.ACTION_GET_CONTENT);
                                } else {
                                    i.setAction(Intent.ACTION_OPEN_DOCUMENT);
                                }
                                startActivityForResult(i, 1);


                                break;

                            case 1:
                                Intent i2 = new Intent();
                                i2.setAction(MediaStore.ACTION_IMAGE_CAPTURE);

                                i2.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mFile));


                                startActivityForResult(i2, 2);//拍照
                                break;
                        }
                    }
                }).show();
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case 1://调取图库照片
                if (null != data) {
                    Uri uri = data.getData();
                    //根据需要，也可以加上Option这个参数
                    InputStream inputStream = null;
                    try {
                        inputStream = getContentResolver().openInputStream(uri);
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    }
                    Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                    mIcon.setImageBitmap(bitmap);
                }
                Intent i = new Intent();
                i.setAction("com.android.camera.action.CROP");
                i.setDataAndType(Uri.fromFile(mFile), "image/*");
                i.putExtra("crop", true);
                //框
                i.putExtra("aspectX", 1);
                i.putExtra("aspectY", 1);
                //大小
                i.putExtra("outputX", 200);
                i.putExtra("outputY", 200);
                i.putExtra("return-data", true);
                startActivityForResult(i, 222);

                break;
            case 2://拍照

//                Bundle bundle = data.getExtras();
//                Bitmap bitmap = (Bitmap) bundle.get("data");
//                mIcon.setImageBitmap(bitmap);


                BitmapFactory.Options opts = new BitmapFactory.Options();
                //只是解析图片的四周  不会加载图片，会加载图片的宽高
                opts.inJustDecodeBounds = true;
                //第一次获得宽高比例
                Bitmap bitmap = BitmapFactory.decodeFile(mFile.getPath(), opts);
                //根据宽高设置
                int w = opts.outWidth;
                int h = opts.outHeight;
                int width = 200;
                //缩放比例
                int ss = w / width;
                //缩放比
                opts.inSampleSize = ss;//  以1/16来缩放。
                //如果为true则没有图片
                opts.inJustDecodeBounds = false;
                //二次采样
                bitmap = BitmapFactory.decodeFile(mFile.getPath(), opts);


//                mIcon.setImageBitmap(bitmap);

                //裁剪
                Intent ii = new Intent();
                ii.setAction("com.android.camera.action.CROP");
                ii.setDataAndType(Uri.fromFile(mFile), "image/*");
                ii.putExtra("crop", true);
                //框
                ii.putExtra("aspectX", 1);
                ii.putExtra("aspectY", 1);
                //大小
                ii.putExtra("outputX", 200);
                ii.putExtra("outputY", 200);
                ii.putExtra("return-data", true);
                startActivityForResult(ii, 222);

                break;
            case 222:
                Bitmap bmp = data.getParcelableExtra("data");
                mIcon.setImageBitmap(bmp);
                break;

        }
    }

    public void addName() {
        mName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                layout = getLayoutInflater().inflate(R.layout.addfood_item, null);
                editText = (EditText) layout.findViewById(R.id.add_content);
                title = (TextView) layout.findViewById(R.id.add_title);
                cancel = (TextView) layout.findViewById(R.id.add_cancel);
                confirm = (TextView) layout.findViewById(R.id.add_confirm);
                final AlertDialog dialog = new
                        AlertDialog.Builder(CreateActivity.this)
                        .setView(layout).show();
                title.setText("菜名");

                cancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                    }
                });
                confirm.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String name = editText.getText().toString();
                        if (name != null) {
                            mName.setText(name);
                            dialog.dismiss();
                        } else {
                            Toast.makeText(CreateActivity.this, "菜名不能为空！", Toast.LENGTH_SHORT).show();
                        }
                    }
                });


            }
        });
    }

    public void addCookName() {
        mShort.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                layout = getLayoutInflater().inflate(R.layout.addfood_item, null);
                editText = (EditText) layout.findViewById(R.id.add_content);
                title = (TextView) layout.findViewById(R.id.add_title);
                cancel = (TextView) layout.findViewById(R.id.add_cancel);
                confirm = (TextView) layout.findViewById(R.id.add_confirm);
                final AlertDialog dialog = new
                        AlertDialog.Builder(CreateActivity.this)
                        .setView(layout).show();
                title.setText("菜谱简介");

                cancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                    }
                });
                confirm.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String name = editText.getText().toString();
                        if (name != null) {
                            mShort.setText(name);
                            dialog.dismiss();
                        } else {
                            Toast.makeText(CreateActivity.this, "菜谱不能为空！", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
            }
        });
    }

    public void addMaterName() {
        mMater.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                layout = getLayoutInflater().inflate(R.layout.addfood_item, null);
                editText = (EditText) layout.findViewById(R.id.add_content);
                title = (TextView) layout.findViewById(R.id.add_title);
                cancel = (TextView) layout.findViewById(R.id.add_cancel);
                confirm = (TextView) layout.findViewById(R.id.add_confirm);
                final AlertDialog dialog = new
                        AlertDialog.Builder(CreateActivity.this)
                        .setView(layout).show();
                title.setText("用料");

                cancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                    }
                });
                confirm.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String name = editText.getText().toString();
                        if (name != null) {
                            mMater.setText(name);
                            dialog.dismiss();
                        } else {
                            Toast.makeText(CreateActivity.this, "用料不能为空！", Toast.LENGTH_SHORT).show();
                        }
                    }
                });


            }
        });
    }


    public void addStepName() {
        mStep.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                layout = getLayoutInflater().inflate(R.layout.addfood_item, null);
                editText = (EditText) layout.findViewById(R.id.add_content);
                title = (TextView) layout.findViewById(R.id.add_title);
                cancel = (TextView) layout.findViewById(R.id.add_cancel);
                confirm = (TextView) layout.findViewById(R.id.add_confirm);
                final AlertDialog dialog = new
                        AlertDialog.Builder(CreateActivity.this)
                        .setView(layout).show();
                title.setText("添加步骤");

                cancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                    }
                });
                confirm.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String name = editText.getText().toString();
                        if (name != null) {
                            mStep.setText(name);
                            dialog.dismiss();
                        } else {
                            Toast.makeText(CreateActivity.this, "步骤不能为空！", Toast.LENGTH_SHORT).show();
                        }
                    }
                });


            }
        });
    }




    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_create, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
